package com.txl.web.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.txl.domain.ContactBean;
import com.txl.domain.ContactGroupBean;
import com.txl.service.ContactGroupService;
import com.txl.service.ContactService;
import com.txl.utils.WebUtils;

public class EditContactServlet extends HttpServlet {

	ContactService service = new ContactService();
	
	//提供修改界面
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String rg = request.getParameter("rg");
		request.setAttribute("rg", rg);
		String id = request.getParameter("id");
		ContactBean c = service.findContact(id);
		request.setAttribute("c", c);
		ContactGroupService cgs = new ContactGroupService();
		List<ContactGroupBean> list = cgs.get2All();//获取2级节点
		request.setAttribute("list", list);
		request.getRequestDispatcher("/page/editcontact.jsp").forward(request, response);
		
	}

	//处理修改请求
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try{
			ContactBean c = WebUtils.request2Bean(request, ContactBean.class);
			service.updateContact(c);
			request.setAttribute("message", "更新成功！！");
		}catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("message", "更新失败！！");
		}
		request.getRequestDispatcher("/message.jsp").forward(request, response);
	}

}

